package com.ruoyi.web.controller.business;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.file.ExcelExporter;
import com.ruoyi.system.domain.ChuKuDanDetail;
import com.ruoyi.system.domain.DingDanDetail;
import com.ruoyi.system.domain.vo.ChuKuDanAddRequest;
import com.ruoyi.system.domain.vo.ChuKuDanAuditRequest;
import com.ruoyi.system.domain.vo.ChuKuDanQuery;
import com.ruoyi.system.service.IChuKuDanDetailService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 出库单信息 前端控制器
 * </p>
 *
 * @author qubaofeng
 * @since 2025-02-21
 */
@RestController
@RequestMapping("/system/chuKuDan")
public class ChuKuDanController extends BaseController {


    @Resource
    private IChuKuDanDetailService service;

    /**
     * 获取列表
     */
    @GetMapping("/listWithPage")
    public TableDataInfo listWithPage(ChuKuDanQuery req)
    {
        startPage();
        List<DingDanDetail> list = service.selectChuKuDanDetailList(req);
        return getDataTable(list);
    }

    /**
     * 导出列表，不分页
     */
    @PostMapping("/export")
    public void export(ChuKuDanQuery req, HttpServletResponse rsp) throws IOException {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        List<DingDanDetail> list = service.selectChuKuDanDetailList(req);

        // 转换为List<Map<String, Object>>
        List<List<String>> datas = new ArrayList<>();
        List<String> titles = new ArrayList<>();
        titles.add("档口");
        titles.add("仓库");
        titles.add("货品");
        titles.add("订单日期");
        titles.add("订单时间");
        titles.add("出库时间");
        titles.add("计划数量");
        titles.add("实际出库数量");

        datas.add(titles);
        for (DingDanDetail detail : list) {
            List<String> data = new ArrayList<>();
            data.add(detail.getDangKouName());
            data.add(detail.getCangKuName());
            data.add(detail.getHuoPinName()+"("+detail.getHuoPinId()+")");
            data.add(detail.getRiQi().format(formatter));
            data.add(detail.getShiJian());
            data.add(detail.getCreateTime().toString());
            data.add(detail.getBuyCount().toPlainString());
            data.add(detail.getRealBuyCount().toPlainString());

            datas.add(data);
        }

        ExcelExporter.exportToExcel(datas, rsp.getOutputStream());
    }


    /**
     * 获取详情
     */
    @GetMapping("/{id}")
    public AjaxResult getDetail(@PathVariable(value = "id") Long id)
    {
        ChuKuDanDetail ruKuDan = service.getChuKuDanDetail(id);
        return AjaxResult.success(ruKuDan);
    }


    /**
     * 新增
     */
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody ChuKuDanAddRequest request)
    {
        return toAjax(service.add(request, getUsername()));
    }


    /**
     * 修改
     */
    @PostMapping("/update")
    public AjaxResult update(@Validated @RequestBody ChuKuDanDetail request)
    {
        return toAjax(service.modify(request, getUsername()));
    }

    /**
     * 修改
     */
    @PutMapping("/audit")
    public AjaxResult audit(@Validated @RequestBody ChuKuDanAuditRequest request)
    {
        boolean ok = service.audit(request, getUsername());
        if(ok){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }

    }

    @PostMapping("/tuiKu")
    public AjaxResult tuiKu(@RequestBody ChuKuDanDetail info){
        boolean ok = service.tuiKu(info);
        if(ok){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    @PostMapping("/tuiHuo")
    public AjaxResult tuiHuo(@RequestBody ChuKuDanDetail info){



        boolean ok = service.tuiHuo(info);
        if(ok){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }


}
